package com.zc.portal.iface.repository.impl;

import com.zc.portal.domain.dto.SysUrlPattenAttribute;
import com.zc.portal.iface.repository.UrlPattenAttributeRepositroy;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Repository(value = "urlPattenAttributeRepositroy")
public class UrlPattenAttributeRepositroyImpl implements UrlPattenAttributeRepositroy {

    @Autowired
    private SqlSessionTemplate sessionTemplate;

    @Override
    public List<SysUrlPattenAttribute> queryUrlPattenAttributes() {
        return sessionTemplate.selectList(NAME_SPACE + "queryUrlPattenAttributes");
    }

    @Override
    public Map<String, Set<ConfigAttribute>> getUrlConfigAttributeMap() {
        List<SysUrlPattenAttribute> list = this.queryUrlPattenAttributes();
        Map<String, Set<ConfigAttribute>> map = list.stream().collect(
                Collectors.groupingBy(SysUrlPattenAttribute::getUrlPatten,
                        Collectors.mapping(SysUrlPattenAttribute::getConfigAttribute,
                                Collectors.toSet())));
        return map;
    }

    @Override
    public int addUrlConfigAttribute(SysUrlPattenAttribute urlPattenAttribute) {
        return sessionTemplate.insert(NAME_SPACE + "addUrlConfigAttribute", urlPattenAttribute);
    }
}
